import { create } from 'zustand';

const mockPromotions = [
  {
    id: 1,
    slug: 'summer-sale-2024',
    name: '夏季特惠活动',
    description: '夏季精酿啤酒促销活动，全场8折优惠',
    linkUrl: '/pages/promotion/detail?id=1',
    linkType: 'internal',
    image: 'https://images.unsplash.com/photo-1535958636474-b021ee887b13?w=800&h=400&fit=crop',
    status: 'active',
    weight: 100,
    type: 'banner',
    createdAt: '2024-03-15 10:00:00',
    updatedAt: '2024-03-15 10:00:00'
  },
  {
    id: 2,
    slug: 'app-launch-splash',
    name: 'APP启动页推广',
    description: '新品上市宣传启动页',
    linkUrl: 'https://www.example.com/promo',
    linkType: 'external',
    image: 'https://images.unsplash.com/photo-1571613316887-6f8d5cbf7ef7?w=800&h=400&fit=crop',
    status: 'active',
    weight: 90,
    type: 'splash',
    createdAt: '2024-03-14 09:00:00',
    updatedAt: '2024-03-14 09:00:00'
  },
  {
    id: 3,
    slug: 'new-arrival-banner',
    name: '新品上市横幅',
    description: '展示最新上架的精酿啤酒',
    linkUrl: '/pages/product/list?category=new',
    linkType: 'internal',
    image: 'https://images.unsplash.com/photo-1608270586620-248524c67de9?w=800&h=400&fit=crop',
    status: 'inactive',
    weight: 80,
    type: 'banner',
    createdAt: '2024-03-13 14:00:00',
    updatedAt: '2024-03-13 14:00:00'
  },
  {
    id: 4,
    slug: 'membership-splash',
    name: '会员专享活动',
    description: '会员日专属优惠活动启动页',
    linkUrl: '/pages/member/benefits',
    linkType: 'internal',
    image: 'https://images.unsplash.com/photo-1436076863939-06870fe779c2?w=800&h=400&fit=crop',
    status: 'active',
    weight: 95,
    type: 'splash',
    createdAt: '2024-03-12 11:00:00',
    updatedAt: '2024-03-12 11:00:00'
  },
  {
    id: 5,
    slug: 'oktoberfest-banner',
    name: '啤酒节特别活动',
    description: '慕尼黑啤酒节主题活动，德式精酿限时优惠',
    linkUrl: '/pages/event/oktoberfest',
    linkType: 'internal',
    image: 'https://images.unsplash.com/photo-1508253730651-e5ace80a7025?w=800&h=400&fit=crop',
    status: 'active',
    weight: 85,
    type: 'banner',
    createdAt: '2024-03-10 10:00:00',
    updatedAt: '2024-03-10 10:00:00'
  },
  {
    id: 6,
    slug: 'weekend-special',
    name: '周末特价',
    description: '每周五至周日，精选啤酒买二送一',
    linkUrl: '/pages/promotion/weekend',
    linkType: 'internal',
    image: 'https://images.unsplash.com/photo-1600788886242-5c96aabe3757?w=800&h=400&fit=crop',
    status: 'inactive',
    weight: 70,
    type: 'banner',
    createdAt: '2024-03-08 09:00:00',
    updatedAt: '2024-03-08 09:00:00'
  }
];

const usePromotionsStore = create((set, get) => ({
  // 状态
  promotions: [],
  loading: false,
  filters: {
    status: 'all',
    type: 'all',
    searchText: ''
  },

  // 获取推广列表
  getPromotions: async (filters = {}) => {
    set({ loading: true });
    
    try {
      // 模拟API调用延迟
      await new Promise(resolve => setTimeout(resolve, 500));
      
      let filtered = [...mockPromotions];
      
      // 应用筛选
      if (filters.status && filters.status !== 'all') {
        filtered = filtered.filter(p => p.status === filters.status);
      }
      
      if (filters.type && filters.type !== 'all') {
        filtered = filtered.filter(p => p.type === filters.type);
      }
      
      if (filters.searchText) {
        const searchLower = filters.searchText.toLowerCase();
        filtered = filtered.filter(p =>
          p.name.toLowerCase().includes(searchLower) ||
          p.description.toLowerCase().includes(searchLower) ||
          p.slug.toLowerCase().includes(searchLower)
        );
      }
      
      // 按权重排序
      filtered.sort((a, b) => b.weight - a.weight);
      
      set({ 
        promotions: filtered,
        filters: { ...get().filters, ...filters },
        loading: false 
      });
      
      return filtered;
    } catch (error) {
      set({ loading: false });
      throw error;
    }
  },

  // 创建推广
  createPromotion: async (promotion) => {
    set({ loading: true });
    
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      const newPromotion = {
        ...promotion,
        id: Date.now(),
        createdAt: new Date().toLocaleString(),
        updatedAt: new Date().toLocaleString()
      };
      
      mockPromotions.push(newPromotion);
      await get().getPromotions(get().filters);
      
      return newPromotion;
    } catch (error) {
      set({ loading: false });
      throw error;
    }
  },

  // 更新推广
  updatePromotion: async (id, updates) => {
    set({ loading: true });
    
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      const index = mockPromotions.findIndex(p => p.id === id);
      if (index !== -1) {
        mockPromotions[index] = {
          ...mockPromotions[index],
          ...updates,
          updatedAt: new Date().toLocaleString()
        };
      }
      
      await get().getPromotions(get().filters);
      return mockPromotions[index];
    } catch (error) {
      set({ loading: false });
      throw error;
    }
  },

  // 删除推广
  deletePromotion: async (id) => {
    set({ loading: true });
    
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      const index = mockPromotions.findIndex(p => p.id === id);
      if (index !== -1) {
        mockPromotions.splice(index, 1);
      }
      
      await get().getPromotions(get().filters);
    } catch (error) {
      set({ loading: false });
      throw error;
    }
  },

  // 切换推广状态
  togglePromotionStatus: async (id, status) => {
    return await get().updatePromotion(id, { status });
  },

  // 设置筛选条件
  setFilters: (filters) => {
    set({ filters: { ...get().filters, ...filters } });
    get().getPromotions({ ...get().filters, ...filters });
  },

  // 清空筛选条件
  clearFilters: () => {
    const defaultFilters = {
      status: 'all',
      type: 'all',
      searchText: ''
    };
    set({ filters: defaultFilters });
    get().getPromotions(defaultFilters);
  }
}));

export default usePromotionsStore;